package org.tnaggs.project.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.tnaggs.project.manager.PlayerManager;
import org.tnaggs.project.manager.TeamManager;
import org.tnaggs.project.model.Player;
import org.tnaggs.project.model.Team;

@Controller
@RequestMapping("/player")
public class PlayerController {

	@Autowired
	private PlayerManager playerManager;
	
	@Autowired
	private TeamManager teamManager;
	
	@RequestMapping("addPlayer.html*")
	public String addPlayer(@RequestParam("id") int teamID,@ModelAttribute("player")
    Player newVal, BindingResult result) {
		Team team = teamManager.getTeam(teamID);
		
		newVal.setTeamID(teamID);
        playerManager.createPlayer(newVal);
		
		return "redirect:/team/view.html?name="+team.getEncodedName();
	}
	
}
